<?php
/**
 * Clase Base DP para BaseSoDeliveryLineaDP
 *
 * @package    
 * @subpackage BaseSoDeliveryLineaDP
 * @author     hans
 * @version    SVN:: BaseSoDeliveryLineaDP.class.php,v 1.1 2012-05-01 22:33:43 Hans.parra Exp $
 */

require_once('class/common/arrayClass.class.php');
require_once('class/im/dlv/BN/SoDeliveryLineaBN.class.php');

abstract class BaseSoDeliveryLineaDP extends SoDeliveryLineaBN
{
  /* N�mero de registros afectados */
  private $nRowsAffected;

  public function __construct($bSetDefault = true)
  {
    if( $bSetDefault instanceof SoDeliveryLineaBN )
    {
      $o_SoDeliveryLineaBN = $bSetDefault;
      if($o_SoDeliveryLineaBN->getDlvId()) : $this->setDlvId($o_SoDeliveryLineaBN->getDlvId()); endif;
      if($o_SoDeliveryLineaBN->getLineaId()) : $this->setLineaId($o_SoDeliveryLineaBN->getLineaId()); endif;
      if($o_SoDeliveryLineaBN->getArtId()) : $this->setArtId($o_SoDeliveryLineaBN->getArtId()); endif;
      if($o_SoDeliveryLineaBN->getArtNombre()) : $this->setArtNombre($o_SoDeliveryLineaBN->getArtNombre()); endif;
      if($o_SoDeliveryLineaBN->getAlmId()) : $this->setAlmId($o_SoDeliveryLineaBN->getAlmId()); endif;
      if($o_SoDeliveryLineaBN->getCant()) : $this->setCant($o_SoDeliveryLineaBN->getCant()); endif;
      if($o_SoDeliveryLineaBN->getUoM()) : $this->setUoM($o_SoDeliveryLineaBN->getUoM()); endif;
      if($o_SoDeliveryLineaBN->getCosto()) : $this->setCosto($o_SoDeliveryLineaBN->getCosto()); endif;
      if($o_SoDeliveryLineaBN->getObs()) : $this->setObs($o_SoDeliveryLineaBN->getObs()); endif;
      if($o_SoDeliveryLineaBN->getCreaUsr()) : $this->setCreaUsr($o_SoDeliveryLineaBN->getCreaUsr()); endif;
      if($o_SoDeliveryLineaBN->getCreaFecha()) : $this->setCreaFecha($o_SoDeliveryLineaBN->getCreaFecha()); endif;
      if($o_SoDeliveryLineaBN->getModifUsr()) : $this->setModifUsr($o_SoDeliveryLineaBN->getModifUsr()); endif;
      if($o_SoDeliveryLineaBN->getModifFecha()) : $this->setModifFecha($o_SoDeliveryLineaBN->getModifFecha()); endif;
    }
  }

  /* Obtiene el n�mero de registros afectados */
  public function getRowsAffected()
  {
    return $this->nRowsAffected;
  }

  public function save()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();

      if($this->getDlvId() && $this->getLineaId()){
        $a_Fields = array();

        if($this->getDlvId()) : $a_Fields['dlvl_dlvId'] = ($this->getDlvId()); endif;
        if($this->getLineaId()) : $a_Fields['dlvl_lineaId'] = ($this->getLineaId()); endif;
        if($this->getArtId()) : $a_Fields['dlvl_artId'] = ($this->getArtId()); endif;
        if($this->getArtNombre()) : $a_Fields['dlvl_artNombre'] = ($this->getArtNombre()); endif;
        if($this->getAlmId()) : $a_Fields['dlvl_almId'] = ($this->getAlmId()); endif;
        if($this->getCant()) : $a_Fields['dlvl_cant'] = ($this->getCant()); endif;
        if($this->getUoM()) : $a_Fields['dlvl_UoM'] = ($this->getUoM()); endif;
        if($this->getCosto()) : $a_Fields['dlvl_costo'] = ($this->getCosto()); endif;
        if($this->getObs()) : $a_Fields['dlvl_obs'] = ($this->getObs()); endif;
        if($this->getCreaUsr()) : $a_Fields['dlvl_creaUsr'] = ($this->getCreaUsr()); endif;
        if($this->getCreaFecha()) : $a_Fields['dlvl_creaFecha'] = ($this->getCreaFecha()); endif;
        if($this->getModifUsr()) : $a_Fields['dlvl_modifUsr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['dlvl_modifFecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_keys(',', $a_Fields);
        $sStringValue = "'".implode("','", $a_Fields)."'";

        $sql = "INSERT INTO so_delivery_linea (".$sStringField.") VALUES (".$sStringValue.");";
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows();
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function update()
  {
    try
    {
      if($this->getDlvId() && $this->getLineaId())
      {
        $a_Fields = array();

        if($this->getDlvId()) : $a_Fields['dlvl_dlvId'] = ($this->getDlvId()); endif;
        if($this->getLineaId()) : $a_Fields['dlvl_lineaId'] = ($this->getLineaId()); endif;
        if($this->getArtId()) : $a_Fields['dlvl_artId'] = ($this->getArtId()); endif;
        if($this->getArtNombre()) : $a_Fields['dlvl_artNombre'] = ($this->getArtNombre()); endif;
        if($this->getAlmId()) : $a_Fields['dlvl_almId'] = ($this->getAlmId()); endif;
        if($this->getCant()) : $a_Fields['dlvl_cant'] = ($this->getCant()); endif;
        if($this->getUoM()) : $a_Fields['dlvl_UoM'] = ($this->getUoM()); endif;
        if($this->getCosto()) : $a_Fields['dlvl_costo'] = ($this->getCosto()); endif;
        if($this->getObs()) : $a_Fields['dlvl_obs'] = ($this->getObs()); endif;
        if($this->getCreaUsr()) : $a_Fields['dlvl_creaUsr'] = ($this->getCreaUsr()); endif;
        if($this->getCreaFecha()) : $a_Fields['dlvl_creaFecha'] = ($this->getCreaFecha()); endif;
        if($this->getModifUsr()) : $a_Fields['dlvl_modifUsr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['dlvl_modifFecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_key_value(',', $a_Fields);

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "UPDATE so_delivery_linea SET ".$sStringField." WHERE 1 AND dlvl_dlvId='".$this->getDlvId()."' AND dlvl_lineaId='".$this->getLineaId()."';";
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows();
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function delete()
  {
    try
    {
      if($this->getDlvId() && $this->getLineaId())
      {
        $a_Fields = array();

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "DELETE FROM so_delivery_linea WHERE 1 AND dlvl_dlvId='".$this->getDlvId()."' AND dlvl_lineaId='".$this->getLineaId()."';"; 
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  /* B�squeda del registro por la llave primaria */
  public static function selectByPK($vDlvId, $vLineaId)
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT dlvl_dlvId, dlvl_lineaId, dlvl_artId, dlvl_artNombre, dlvl_almId, dlvl_cant, dlvl_UoM, dlvl_costo, dlvl_obs, dlvl_creaUsr, dlvl_creaFecha, dlvl_modifUsr, dlvl_modifFecha FROM so_delivery_linea WHERE 1 AND dlvl_dlvId='".$vDlvId."' AND dlvl_lineaId='".$vLineaId."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      
      return $o_Propel?self::insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_SoDeliveryLineaDP = new SoDeliveryLineaBN();
    $o_SoDeliveryLineaDP->setDlvId($o_propel->dlvl_dlvId);
    $o_SoDeliveryLineaDP->setLineaId($o_propel->dlvl_lineaId);
    $o_SoDeliveryLineaDP->setArtId($o_propel->dlvl_artId);
    $o_SoDeliveryLineaDP->setArtNombre($o_propel->dlvl_artNombre);
    $o_SoDeliveryLineaDP->setAlmId($o_propel->dlvl_almId);
    $o_SoDeliveryLineaDP->setCant($o_propel->dlvl_cant);
    $o_SoDeliveryLineaDP->setUoM($o_propel->dlvl_UoM);
    $o_SoDeliveryLineaDP->setCosto($o_propel->dlvl_costo);
    $o_SoDeliveryLineaDP->setObs($o_propel->dlvl_obs);
    $o_SoDeliveryLineaDP->setCreaUsr($o_propel->dlvl_creaUsr);
    $o_SoDeliveryLineaDP->setCreaFecha($o_propel->dlvl_creaFecha);
    $o_SoDeliveryLineaDP->setModifUsr($o_propel->dlvl_modifUsr);
    $o_SoDeliveryLineaDP->setModifFecha($o_propel->dlvl_modifFecha);
    return $o_SoDeliveryLineaDP;
  }
}